package sz.szsmk.citizencard.ui.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;

import com.chenenyu.router.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.NoticeListActivityBinding;
import sz.szsmk.citizencard.dialog.TwoButtonDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.NoticeListRes;
import sz.szsmk.citizencard.mvp.contract.INoticeListContact;
import sz.szsmk.citizencard.mvp.presenter.NoticeListPresenter;
import sz.szsmk.citizencard.ui.adapter.NoticeListAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/8/13 17:09
 * description:
 */
@Route("noticelist")
public class NoticeListActivity extends IBaseActivity<NoticeListPresenter, NoticeListActivityBinding>
        implements INoticeListContact.INoticeListView, View.OnClickListener {

    private NoticeListAdapter noticeListAdapter;
    private String messageType;
    private String word;
    private List<NoticeListRes.MessageDetailListBean> mList = new ArrayList<>();

    private int currentPager = 1;

    @Override
    protected int attachLayoutRes() {
        return R.layout.notice_list_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        String title = getIntent().getStringExtra("title");
        messageType = getIntent().getStringExtra("messageType");
        if (messageType == null) {
            messageType = "";
        }
        word = getIntent().getStringExtra("word");
        mPresenter.changesIsRead(messageType, "1");
        dataBinding.titleLayout.settingTitle.setText(title);
        dataBinding.titleLayout.shareIv.setVisibility(View.VISIBLE);
        dataBinding.titleLayout.shareIv.setImageResource(R.drawable.adress_del);
        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.titleLayout.shareIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDelDialog();
            }
        });
        noticeListAdapter = new NoticeListAdapter(this);
        noticeListAdapter.setDelItemCallBack(new NoticeListAdapter.DelitemCallBack() {
            @Override
            public void onDelItemCallBack(int position) {
                NoticeListRes.MessageDetailListBean detailListBean = mList.get(position);
                String id = detailListBean.getDetailId();
                mPresenter.delItem(id);

                removeData(position);
            }
        });

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPager = 1;
                if (word != null) {
                    //搜索 word为用户输入内容
                    mPresenter.getNoticeList(currentPager, word, messageType);
                } else {
                    //点击 word可输入为空
                    mPresenter.getNoticeList(currentPager, "", messageType);
                }
            }
        });
        dataBinding.swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getNoticeList(currentPager, "", messageType);
            }
        });
        dataBinding.noticeListRv.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.noticeListRv.setAdapter(noticeListAdapter);
        dataBinding.swipeRefreshLayout.autoRefresh();
    }

    private void showDelDialog() {
        final TwoButtonDialog dialog = new TwoButtonDialog(this, "确认删除全部条目吗");
        dialog.setOnOkClickListener(new TwoButtonDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                dialog.cancel();
            }
        });
        dialog.setOnCancelClickListener(new TwoButtonDialog.OnCancelClickListener() {
            @Override
            public void onCancelClick() {
                dialog.cancel();
                mPresenter.clearItem(messageType);
            }
        });
        dialog.show();
    }

    public void removeData(int position) {
        mList.remove(position);
//        删除动画
        noticeListAdapter.notifyItemRemoved(position);
        noticeListAdapter.notifyDataSetChanged();
    }

    @Override
    protected void updateViews() {
    }

    @Override
    public void getNoticeListError(String response) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void getNoticeListSuccess(List<NoticeListRes.MessageDetailListBean> response) {

        if (currentPager == 1)
            this.mList.clear();
        this.mList.addAll(response);
        noticeListAdapter.setItemData(mList);
        noticeListAdapter.notifyDataSetChanged();

        boolean hasMore = response.size() == 10;
        if (hasMore) {
            currentPager++;
        }else{
            //完成加载并标记没有更多数据
            dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
        }

        reFreshLayoutFinish();
    }

    private void reFreshLayoutFinish(){
        dataBinding.swipeRefreshLayout.finishRefresh();
        dataBinding.swipeRefreshLayout.finishLoadMore();
    }
    @Override
    public void changesIsReadFail() {

    }

    @Override
    public void changesIsReadSucc() {

    }

    @Override
    public void delItemSucc() {

    }

    @Override
    public void delItemFail() {

    }

    @Override
    public void clearItemSucc() {
        mPresenter.getNoticeList(currentPager, "", messageType);
    }

    @Override
    public void clearItemFail() {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }
}
